<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" destroy-on-close @closed="$emit('closed')" fullscreen :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="生产单号" prop="production_code">
                        <el-input v-model="form.production_code" placeholder="生产单号(不填写自动生成)" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="生产日期" prop="open_date">
                        <el-date-picker v-model="form.open_date" type="date" placeholder="请选择日期" :value-format="'YYYY-MM-DD'" style="width:100%" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="倒冲领料" prop="back_flush">
                        <el-switch v-model="form.back_flush" :active-value="1" :inactive-value="0" disabled></el-switch>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="仓库" prop="ware_id">
                        <el-select v-model="form.ware_id" filterable style="width: 100%" placeholder="仓库" @change="wareChange" clearable>
                            <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="24">
                <el-form-item label="产品清单" prop="detail">
                    <!-- <el-button type="primary" size="small" icon="el-icon-plus" @click="checkGoods">选择产品</el-button> -->
                    <el-button type="primary" size="small" icon="el-icon-plus" @click="checkSale">选择销售单产品</el-button>
                    <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="700" hideAdd show-summary>
                        <el-table-column style="background: #f00" prop="contract_code" label="销售单号" align="center" width="100">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.contract_code}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="产品型号" align="center">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.goods_model}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="颜色" align="center" width="100">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.color_name}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="方向" align="center" width="100">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.direction_name}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="速比" align="center" width="100">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.speed_name}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="法兰" align="center" width="100">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.flange_name}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="孔数" align="center" width="80">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.hole}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="number" label="数量" align="center" width="80">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.number}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="使用现货" align="center" width="100">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.is_stock ? '是' : '否'}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="客户" align="center">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.customer_name}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="备注" align="center">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.remark1}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="备注2" align="center">
                            <template #default="scope">
                                <div :style="'color:' + (scope.row.is_stock == 1 ? '#F00' : '')">
                                    {{scope.row.remark2}}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column label="是否装配" align="center" width="100">
                            <template #default="scope">
                                <el-switch v-model="scope.row.is_assembly" :active-value="1" active-color="#13ce66" :inactive-value="0" inactive-color="#ff4949" inline-prompt active-text="是" inactive-text="否" :disabled="scope.row.is_stock == 1"></el-switch>
                            </template>
                        </el-table-column>
                        <el-table-column label="是否包装" align="center" width="100">
                            <template #default="scope">
                                <el-switch v-model="scope.row.is_packaging" :active-value="1" active-color="#13ce66" :inactive-value="0" inactive-color="#ff4949" inline-prompt active-text="是" inactive-text="否"></el-switch>
                            </template>
                        </el-table-column>
                    </sc-form-table>
                </el-form-item>
            </el-col>
        </el-form>
        <el-footer style="text-align: right;position: fixed;bottom: 0;right: 0;left: 0;padding: 10px 20px;background: #fff;border-top: 1px solid #eee;">
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </el-footer>
	</el-dialog>
    <select-contract v-if="dialog.save" ref="saveDialog" @success="slaeUpdate" @closed="dialog.save=false" :query="{notId: notId}"></select-contract>
</template>

<script>
import SelectContract from '@/views/sale/contract/selectContract.vue';
export default {
    components: { SelectContract },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增生产任务',
				edit: '编辑生产任务',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id: "",
                production_code: "",
                open_date: this.moment().format('YYYY-MM-DD'),
                detail: [],
                back_flush: 1,
                ware_id: "",
			},
			//验证规则
			// rules: {
			// 	contract_id: [{ required: true, message: '请选择销售' }],
			// },
            contract_code: "",
            addTemplate:{},
            dialog:{
                goods: false,
            },
            wareList: [],
            notId: [],
		}
	},
	mounted() {
        this.getWare()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.production.plan.edit.post(this.form);
					} else {
						res = await this.$API.production.plan.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
        goProduction(data){
            this.form.contract_id = data.id
            this.contract_code = data.contract_code
            let array = []
            data.product.map(item=>{
                array.push({
                    goods_id:item.goods_id,
                    goods_name:item.goods.goods_name,
                    goods_spec:item.goods.goods_spec,
                    goods_model:item.goods.goods_model,
                    unit_name:item.goods.unit.unit_name,
                    goods_code:item.goods.goods_code,
                })
                this.form.detail = array
            })
        },
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            var res = await this.$API.production.plan.detail.get({id:data.id});
            this.form.production_code = data.production_code
            this.form.open_date = data.open_date
            this.form.back_flush = res.data.back_flush
            this.form.ware_id = res.data.ware_id
            let array = []
            res.data.detail.map(item=>{
                array.push({
                    id:item.id,
                    goods_id:item.goods_id,
                    order_item_id:item.order_item_id,
                    contract_code:item.contract_code,
                    goods_model:item.goods && item.goods.goods_model,
                    speed_name:item.goods && item.goods.speedRatio && item.goods.speedRatio.name,
                    flange_name:item.goods && item.goods.flange && item.goods.flange.name,
                    is_stock:item.is_stock,
                    color_name:item.color_name,
                    direction_name:item.direction_name,
                    hole:item.hole,
                    number:item.number,
                    customer_id:item.customer_id,
                    customer_name:item.customer.customer_name,
                    remark1:item.remark1,
                    remark2:item.remark2,
                    is_assembly:item.is_assembly,
                    is_packaging:item.is_packaging,
                    speed_ratio_id: item.speed_ratio_id,
                    flange_id: item.flange_id,
                    color_type: item.color_type,
                    direction_type: item.direction_type,
                })
            })
            this.form.detail = array
		},
        createProduct(data){
            console.log('data :>> ', data);
            this.form.contract_id = data.contract_id
            this.contract_code = data.contract_code
            let array = []
            data.data.map(item=>{
                array.push({
                    goods_id:item.goods_id,
                    goods_name:item.goods_name,
                    unit_name:item.unit_name,
                    goods_code:item.goods_code,
                    goods_model:item.goods_model,
                    goods_spec:item.goods_spec,
                    number:item.number,
                })
            })
            this.form.detail = array
        },
        onFocus(event){
            event.target.select();
        },
        // 选择产品
        checkSale(){
            let array = []
            array = this.form.detail.map(item=>{
                return item.list_id
            })
            this.notId = array
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('add')
            })
        },
        slaeUpdate(data){
            let array = []
            data.map((item)=>{
                let exist = this.common.isExist(item.id, this.form.detail, 'list_id')
				if (exist) {
					return
				}
                array.push({
                    goods_id:item.goods_id,
                    list_id:item.id,
                    order_item_id:item.order_item_id,
                    contract_code:item.contract_code,
                    goods_model:item.goods && item.goods.goods_model,
                    speed_name:item.goods && item.goods.speedRatio && item.goods.speedRatio.name,
                    flange_name:item.goods && item.goods.flange && item.goods.flange.name,
                    is_stock:item.is_stock,
                    color_name:item.color_name,
                    direction_name:item.direction_name,
                    speed_ratio_id: item.speed_ratio_id,
                    flange_id: item.flange_id,
                    color_id: item.color_id,
                    color_type: item.color_type,
                    direction_type: item.direction_type,
                    hole:item.hole,
                    number:item.sale_num,
                    customer_id:item.customer_id,
                    customer_name:item.customer_name,
                    remark1:item.remark1,
                    remark2:item.remark2,
                    is_assembly: item.is_stock ? 0 : 1,
                    is_packaging: 1
                })
            })
            this.form.detail = [...this.form.detail,...array]
        },
        // 获取仓库
        async getWare(){
            var res = await this.$API.goods.ware.all.get();
            this.wareList = res.data;
            res.data.map(item=>{
                if(item.is_default == 1){
                    this.form.ware_id = item.id
                }
            })
        },
	}
}
</script>

<style></style>
